package com.homeos;

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.flotandroidchart.flot.FlotChartContainer;
import com.flotandroidchart.flot.FlotDraw;
import com.flotandroidchart.flot.Options;
import com.flotandroidchart.flot.data.PointData;
import com.flotandroidchart.flot.data.SeriesData;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class Different_Grap_Types extends Activity {
	SeriesData sd1;FlotDraw  fd;FlotChartContainer tv;
	Vector<PointData> pds;Vector<SeriesData> sds;
	static int window_length;
	static double powerlog[];
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        
 
        sds = new Vector<SeriesData>();
        		
		 sd1 = new SeriesData();
		 pds = new Vector<PointData>();
		for(int i=0; i < window_length; i+=1) {
			pds.add(new PointData(i, powerlog[i])); 
		}
		sd1.setData(pds);
		sd1.label="Total Power Usage";
		
		sd1.series.lines.setShow(true);
		sd1.series.lines.fill = true;
		sd1.series.points.show = true;
		sds.add(sd1);
		
		/*
		SeriesData sd2 = new SeriesData();	
		sd2.label="B";
		sd2.setData(new double[][]{{0, 3}, {4, 8}, {8, 5}, {9, 13}});
		sd2.series.bars.show = true;
		sds.add(sd2); 
		
		SeriesData sd3 = new SeriesData();
		sd3.label="C";
		Vector<PointData> pds1 = new Vector<PointData>();
		for(double i=0; i < 14; i+=0.5) {
			pds1.add(new PointData(i, Math.cos(i)));
		}
		sd3.setData(pds1);
		sd3.series.lines.fill = true;
		sds.add(sd3);
		
		SeriesData sd4 = new SeriesData();
		sd4.label="D";
		Vector<PointData> pds2 = new Vector<PointData>();
		for(double i=0; i < 14; i+=0.1) {
			pds2.add(new PointData(i, Math.sqrt(i * 10)));
		}
		sd4.setData(pds2);
		sd4.series.lines.setShow(true);
		sds.add(sd4);
		
		SeriesData sd5 = new SeriesData();
		sd5.label="E";
		Vector<PointData> pds3 = new Vector<PointData>();
		for(double i=0; i < 14; i+=0.5) {
			pds3.add(new PointData(i, Math.sqrt(i)));
		}
		sd5.setData(pds3);
		sd5.series.lines.setShow(true);
		sd5.series.points.show = true;
		sds.add(sd5);
		
		/*
		SeriesData sd6 = new SeriesData();
		sd6.label="F";
		Vector<PointData> pds4 = new Vector<PointData>();
		for(double i=0; i < 14; i+=0.5 + Math.random()) {
			pds4.add(new PointData(i, Math.sqrt(2*i + Math.sin(i) + 5)));
		}
		sd6.setData(pds4);
		sd6.series.lines.setShow(true);
		sd6.series.lines.steps = true;
		sds.add(sd6);
		*/
		
		fd = new FlotDraw(sds, null, null);
        
        setContentView(R.layout.chart);
         tv = (FlotChartContainer)this.findViewById(R.id.flotdraw);
         
        if(tv != null) {
            tv.setDrawData(fd);
        }
    }
    
    
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        	updateUI(intent);       
        }
    };
    
    @Override
	public void onResume() {
		super.onResume();		
		registerReceiver(broadcastReceiver, new IntentFilter(BroadcastService.BROADCAST_ACTION));
		sds = new Vector<SeriesData>();
		sd1 = new SeriesData();
		 pds = new Vector<PointData>();
		for(int i=0; i < window_length; i+=1) {
			pds.add(new PointData(i, powerlog[i] )); 
		}
		
    	sd1.setData(pds);
		sd1.label="Total Power Usage (W)";
		sd1.series.lines.setShow(true);
		sd1.series.lines.fill = true;
		sd1.series.points.show = true;
		sds.add(sd1);
		Options opt = new Options();
		opt.yaxis.ticks = new Integer(10);
		opt.yaxis.max = 1.5* Max(powerlog) + 10;
		opt.yaxis.min = 0;
		opt.xaxis.ticks = new Integer(window_length-1);
		opt.xaxis.max = window_length-1;
		opt.xaxis.min = 0;
		opt.series.points.show = true;
		
		
		
		
		fd = new FlotDraw(sds, opt, null);
        tv = (FlotChartContainer)this.findViewById(R.id.flotdraw);
        if(tv != null) {
            tv.setDrawData(fd);
        }
        
	}
    public void onPause() {
		super.onPause();
		unregisterReceiver(broadcastReceiver);
	
	}	
    
    private void updateUI(Intent intent) {
    	
    	String data = new String(intent.getStringExtra("data")); 
    	
    	for(int i=0; i < window_length - 1; i+=1) {
			powerlog[i] = powerlog[i+1];
		}
    	
    	
    	try
    	{
    		Pattern p = Pattern.compile("<data><socket1><status>(\\d)</status><power>(\\d{1,}\\.\\d{1,})</power></socket1><socket2><status>(\\d)</status><power>(\\d{1,}\\.\\d{1,})</power></socket2><socket3><status>(\\d)</status><power>(\\d{1,}\\.\\d{1,})</power></socket3><socket4><status>(\\d)</status><power>(\\d{1,}\\.\\d{1,})</power></socket4><socket5><status>(\\d)</status><power>(\\d{1,}\\.\\d{1,})</power></socket5><temp>(\\d{1,}\\.\\d{1,}\\s{0,}\\w)</temp><light>(\\d{1,}\\.\\d{1,})</light></data>");
    		Matcher m = p.matcher(data);//(\\d{1,}\\.\\d{1,}\\s\\sC)
    		if(m.find())
    		{
    		
    		
    	
    	
    		 
    		float total = Math.round((Float.parseFloat(m.group(2)) +Float.parseFloat(m.group(4))+Float.parseFloat(m.group(6))
    		+Float.parseFloat(m.group(8))+Float.parseFloat(m.group(10)) )* 100.0)  ;
    		total /= 100.00;
    		powerlog[window_length - 1] = total ;
    		
    		
    		
    		}
    		
    	}	
    	catch(Exception e){Log.d("CHART ERROR",""+e);e.printStackTrace();}
    	
    	sds = new Vector<SeriesData>();
		sd1 = new SeriesData();
		 pds = new Vector<PointData>();
		
		for(int i=0; i < window_length; i+=1) {
			pds.add(new PointData(i, powerlog[i] )); 
		}
		
    	sd1.setData(pds);
    	sd1.label="Total Power Usage (W)";
		sd1.series.lines.setShow(true);
		sd1.series.lines.fill = true;
		sd1.series.points.show = true;
		sds.add(sd1);
		
		Options opt = new Options();
		opt.yaxis.ticks = new Integer(10);
		opt.yaxis.max = 1.5* Max(powerlog) + 10;
		opt.yaxis.min = 0;
		opt.xaxis.ticks = new Integer(window_length-1);
		opt.xaxis.max = window_length-1;
		opt.xaxis.min = 0;
		
		opt.series.points.show = true;
		
		
		
		fd = new FlotDraw(sds, opt, null);
        
        
         tv = (FlotChartContainer)this.findViewById(R.id.flotdraw);
        if(tv != null) {
            tv.setDrawData(fd);
        }
        

    	Log.d("GRAPH", "Data changed "+data);
    	
    	
    }
    
    private double Max(double[] a)
    {
    	double m; int i=0;
    	m = a[i];
    	for(i = 1; i < a.length ; i++)
    	{if(a[i]>m) m = a[i];}
    	
    	return m;
    }
}
